<template src="./template.html"></template>

<script>
import CourseMixins from '../mixins'
import ResizeMixin from 'mixins/resize'
import { PAGE_CONFIG } from 'common/consts'
import { COL_INDEXS, COL_INDEXS_LESSON } from './consts'
import { getStatCourseStudy, getFlushStatCourseStudy } from './models'

export default {
  mixins: [ResizeMixin, CourseMixins],
  data() {
    return {
      COL_INDEXS,
      COL_INDEXS_LESSON,
      isPageLoading: false,

      tableList: [],
      courseList: [],
      PAGE_CONFIG: PAGE_CONFIG,
      pageNo: PAGE_CONFIG.defaultPageNo,
      limit: PAGE_CONFIG.defaultPageSize,
      totalCount: 0,
    };
  },

  methods: {
    $goCourseStat(row) {
      this.$router.push({
        name: 'curriculum.manager.course-stat',
        params: {
          courseId: row.courseId
        },
        query: {
          name: row.studyName
        }
      })
    },
    $goLessonStat(row) {
      this.$router.push({
        name: 'curriculum.manager.lesson-statt',
        params: {
          courseId: row.courseId,
          lessonId: row.lessonId
        },
        query: {
          name: row.studyName
        }
      })
    },
    $handleRecord() {
      this.$router.push({
        name: 'curriculum.manager.record',
        query: {
          courseId: this.courseId
        }
      })
    },
    $search() {
      this.pageNo = 1
      this.$getTableList()
    },
    async $flush() {
      const params = {
        courseId: this.courseId
      };
      try {
        await getFlushStatCourseStudy(params);
        this.$search()
      } catch (err) {
        this.isPageLoading = false;
        this.$message.error(err.message || '刷新异常！')
      }
    },
    async $getTableList() {
      this.isPageLoading = true;
      const params = {
        ...this.filterForm,
        // pageSize: this.pageSize,
        // page: this.pageNo,
        courseId: this.courseId
      };

      try {
        const { data } = await getStatCourseStudy(params);
        const statLs = (data.list || []).map(it => ({
          ...it,
        }))
        this.tableList = (statLs || []).filter(it => parseInt(it.studyType, 10) === 2)
        this.courseList = (statLs || []).filter(it => parseInt(it.studyType, 10) === 1)
        this.totalCount = data.total
        this.isPageLoading = false;
      } catch (err) {
        this.isPageLoading = false;
        this.$message.error(err.message || err.message || '获取机构列表出错')
      }
    },
    $handlePageChange() {
      this.$getTableList();
    },
  },
  created() {
    this.$getTableList();
  },
};
</script>
